package com.feicheng.controller.main.admin.client;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.service.IFClientService;
import com.feicheng.service.IFPayBackService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController("/main/admin/client/deposit-back-request")
@RequestMapping("/main/admin/client/deposit-back-request")
public class DepositBackRequestController {

    @Autowired
    IFClientService clientService;
    
    @Autowired
    IFPayBackService payBackService;

    /**
     * 总店分页查退押记录
     * @param currentPage
     * @param pageSize
     * @param clientName
     * @param name
     * @param statu
     * @param account
     * @param ct
     * @param lastct
     * @param ut
     * @param lastut
     * @param storeId
     * @return
     */
    @GetMapping("/page")
    public ResultInfo getDepositBackRequestByPage(
            @RequestParam(defaultValue="1") Integer currentPage,
            @RequestParam(defaultValue="10") Integer pageSize,
            String clientName,
            String statu,
            Integer storeId
    ){

        JSONObject param = new JSONObject();
        param.put(Constant.ENTITY.STORE.STORE_ID,storeId);
        param.put(Constant.HTTP.PARAM.CURRENT,currentPage);
        param.put(Constant.HTTP.PARAM.SIZE,pageSize);
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU,statu);
        param.put(Constant.ENTITY.CLIENT.CLIENT_NAME,clientName);
        return clientService.getDepositBackRequestByPage(param);
    }

    /**
     * 根据主键取得详细
     * @param backId
     * @return
     */
    @GetMapping("/info")
    public ResultInfo getDepositBackRequest(Integer backId) {
        return clientService.getDepositBackRequest(backId);
    }

    @GetMapping("/all")
    public ResultInfo getDepositBackRequestAll(
    		Integer clientId,
        	Integer storeId,
        	Integer depositId,
        	String statu
        		) {

    		JSONObject param = new JSONObject();
    		param.put(Constant.ENTITY.STORE.STORE_ID, storeId);
    		param.put(Constant.ENTITY.CLIENT.CLIENT_ID, clientId);
    		param.put(Constant.ENTITY.DEPOSIT_RECORD.DEPOSIT_ID, depositId);
    		param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU, statu);
    		
        	return clientService.getDepositBackRequestAll(param);
    }

    @PutMapping("/{backId}/finish")
    public ResultInfo finishDeposit(
            @PathVariable(name="backId") Integer backId) {
    	JSONObject param = new JSONObject();
    	param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID, backId);
        return payBackService.finishDepositRecord(param);
    }

    @PutMapping("/{backId}/reject")
    public ResultInfo rejectDeposit(
            @PathVariable(name="backId") Integer backId,
            String reason) {
    	JSONObject param = new JSONObject();
    	param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID, backId);
    	param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.REASON, reason);
        return payBackService.rejectDepositRecord(param);
    }

    @PutMapping("/{backId}/back")
    public ResultInfo backDeposit(@PathVariable(name="backId") Integer backId) {

    	JSONObject param = new JSONObject();
    	param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID, backId);
    	return payBackService.agreeDepositRecord(param);
    }

    /**
     * 更新退押申请
     * @param backId
     * @param reason
     * @param amount
     * @param statu
     * @return
     */
    @PutMapping("/gai")
    public ResultInfo gaiDepositBackRequest(
            Integer backId,
            String reason,
            Integer amount,
            String statu) {
        JSONObject param = new JSONObject();
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.BACK_ID,backId);
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.REASON,reason);
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.AMOUNT,amount);
        param.put(Constant.ENTITY.DEPOSIT_BACK_REQUEST.STATU,statu);
        clientService.updateDepositBackRequest(param);
        return ResultInfo.success();
    }
}
